package com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.app;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.pay.core.client.AbstractPayCodeMapping;
import com.xiaoshuidi.cloud.framework.pay.core.client.PayCommonResult;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.*;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.MemberBindReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.MemberBindRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.MemberCreateReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.MemberCreateRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.AbstractYinlianPayClient;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.YinlianClientConfig;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayStatusEnum;

import java.math.BigDecimal;

/**
 * 银联App支付接口抽象类
 * @author lee
 */
public abstract class AbstractYinlianAppPayClient extends AbstractYinlianPayClient {
    public AbstractYinlianAppPayClient(Long channelId, String channelCode, YinlianClientConfig config, AbstractPayCodeMapping codeMapping) {
        super(channelId, channelCode, config, codeMapping);
    }

    @Override
    protected PayCommonResult<?> doUnifiedClose(PayCloseUnifiedReqDTO reqDTO) throws Throwable {
        ObjectNode json = baseParams(config.getMerchantCode(), config.getTerminalCode());
        json.put("merOrderId", reqDTO.getMerchantOrderId());
        json.put("instMid", "APPDEFAULT");
        return call("/v1/netpay/close", json, (code, resp) -> GlobalErrorCodeConstants.SUCCESS.isEqualTo(code));
    }



    @Override
    protected ObjectNode unifiedRefundParams(PayRefundUnifiedReqDTO reqDTO) {
        ObjectNode json = baseParams(config.getMerchantCode(), config.getTerminalCode());
        json.put("merOrderId", reqDTO.getPayTradeNo());
        json.put("instMid", "APPDEFAULT");
        json.put("refundAmount", reqDTO.getAmount());
        json.put("refundOrderId", nextOrderNo());
        return json;
    }

    @Override
    public PayCommonResult<PayQueryUnifiedRespDTO<ObjectNode>> unifiedQuery(PayQueryUnifiedReqDTO reqDTO) {
        ObjectNode json = baseParams(config.getMerchantCode(), config.getTerminalCode());
        json.put("instMid", "APPDEFAULT");
        json.put("merOrderId", reqDTO.getMerchantOrderId());
        return call("/v1/netpay/query", json, (code ,resp) -> {
                    PayQueryUnifiedRespDTO<ObjectNode> r = new PayQueryUnifiedRespDTO<ObjectNode>()
                            .setRawResponse(resp)
                            .setMerchantOrderId(reqDTO.getMerchantOrderId());
                    if (GlobalErrorCodeConstants.SUCCESS.isEqualTo(code)) {
                        String tradeStatus = resp.get("status").asText();
                        switch (tradeStatus) {
                            case "TRADE_SUCCESS":
                                r.setStatus(PayStatusEnum.SUCCESS)
                                        .setTransactionId(resp.get("targetOrderId").asText())
                                        .setPayTime(DateUtil.parseLocalDateTime(resp.get("payTime").asText()))
                                        .setPayAmount(BigDecimal.valueOf(resp.get("totalAmount").asDouble() / 100D));
                                return r;
                            case "TRADE_CLOSED":
                                r.setStatus(PayStatusEnum.CLOSED);
                                break;
                            default:
                                r.setStatus(PayStatusEnum.WAITING);
                        }
                    }
                    return r;
                }
        );
    }

}
